package com.chenyi.leetcode.editor.cn;
/**
 * [69]x 的平方根 
 * @author by chenyi
 * @className Sqrtx
 * @date 2022-06-23 21:53:52
 */
public class Sqrtx{
    public static void main(String[] args) {
        Solution solution = new Sqrtx().new Solution();
        System.out.println(solution.mySqrt(8));
    }
    @SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int mySqrt(int x) {
        int l = 0, r = x, ans = -1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if ((long) mid * mid <= x) {
                ans = mid;
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return ans;
    }

    public int mySqrt1(int x) {
        int ans = -1;
        for (int i = 0; i <= x; i++) {
            if ((long) i * i <= x) {
                ans = i;
            } else {
                break;
            }
        }
        return ans;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
